#!/usr/bin/python
# -*- coding: utf-8 -*-


import os
import sys, getopt
import baiduspeech as tts


# default_wavlist = [ 
#     ["主人您好",       "command0"],
#     ["烟机已打开",       "command1"],
#     ["烟机已关闭",       "command2"],
#     ["爆炒功能已启动",     "command3"],
#     ["爆炒功能已关闭",     "command4"],
#     ["煎炸功能已启动",     "command5"],
#     ["煎炸功能已关闭",     "command6"],
#     ["蒸煮功能已启动",     "command7"],
#     ["蒸煮功能已关闭",     "command8"],
#     ["风速已调大",       "command9"],
#     ["风速已调小",       "command10"],
#     ["照明已打开",       "command11"],
#     ["照明已关闭",       "command12"],
#     ["清风功能已打开",     "command13"],
#     ["清风功能已关闭",     "command14"],
#     ["延时关机功能已打开",   "command15"],
#     ["延时关机功能已关闭",   "command16"],
# ]

# default_wavlist = [ 
#     ["打开空调",       "command0"],
#     ["关闭空调",       "command1"],
#     ["温度加一点",       "command2"],
#     ["温度减一点",     "command3"],
#     ["制冷模式",     "command4"],
#     ["制热模式",     "command5"],
#     ["除湿模式",     "command6"],
#     ["自动模式",     "command7"],
#     ["送风模式",     "command8"],
#     ["打开电视",       "command9"],
#     ["关闭电视",       "command10"],
#     ["音量大点",       "command11"],
#     ["音量小点",       "command12"],
#     ["频道加",     "command13"],
#     ["频道减",     "command14"],
# ]

default_wavlist = [ 
    ["discover my smart home devices",    	"audio1"],
	["find devices",           				"audio2"],
    ["discover devices",           			"audio3"],
]


def printhelp(name):
    print 'Usage: %s [OPTION]' % name
    print '-h. --help       help'
    print '-c, --context    input the context which wanted TTS(Text To Speech)'
    print '-o, --ofile      output file name(do not need suffix)'
    print ''


def create_wav(text, file):
    mp3file = file + '.mp3'

    ret = tts.baidu_api(text, mp3file)
    if ret != 0:
        print 'call baidu API failed'
        sys.exit(3)
    else:
        print 'Generate MP3 file'

    wavfile = file + '.wav'

    # Case 1 using pydub, only to 16KHz bitrate
    # sound = AudioSegment.from_mp3(mp3file)
    # sound.export(wavfile, format="wav")
    # wavsound = AudioSegment.from_file(wavfile, format="wav")
    
    # Case 2 using ffmpeg transform from mp3 to wave
    cmd = 'ffmpeg -i %s -f wav -ab 2 -ac 1 -ar 16000 %s' % (mp3file, wavfile)
    os.system(cmd)

    if os.path.isfile(wavfile) == False:
        print 'generate WAV file failed'
        sys.exit(4)
    else:
        print 'Generate WAV file'


def create_mp3(text, file):
    mp3file = file + '.mp3'
    tts.baidu_api(text, mp3file)


def default_create_file():
    for target in default_wavlist:
        if os.path.isfile(target[1]) == True:
            print '%s is exist' % target[1]
        else :
            create_wav(target[0], target[1])


if __name__ == '__main__':

    if len(sys.argv[1:]) == 0:
        default_create_file()
        sys.exit(0)

    dft_text = '请输入需要转换的文字'
    dft_file = 'test'

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hc:o:",["help=", "context=", "ofile="])
    except getopt.GetoptError:
        printhelp(sys.argv[0])
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            printhelp(sys.argv[0])
            sys.exit()
        elif opt in ("-c", "--context"):
            dft_text = arg
        elif opt in ("-o", "--ofile"):
            dft_file = arg

    print 'TTS: %s' % dft_text
    print 'filename: %s' % dft_file

    create_mp3(dft_text, dft_file)

    
